/*
 * Utility.h
 *
 *  Created on: 13/mag/2013
 *      Author: Ialenti Andrea, Marangon Fabio
 */

#include "../headers/Utility.h"

#include <stdlib.h>
#include <stdio.h>

int nameLen = 100;

double* initDblArray(int n) {
	char *funcName = initCharArray(nameLen);
	sprintf(funcName, "initDblArray");

	double *a = (double*) calloc(n, sizeof(double));
	if(a == NULL) {
		fprintf(stdout, "***'%s': memory error during array initialization!\n", funcName);
		freeCharArray(funcName);
		return NULL;
	}
	freeCharArray(funcName);
	return a;
}

void freeDblArray(double *a) {
	free(a);
	a = NULL;
	return;
}

int* initIntArray(int n) {
	char *funcName = initCharArray(nameLen);
	sprintf(funcName, "initIntArray");

	int *a = (int*) calloc(n, sizeof(int));
	if(a == NULL) {
		fprintf(stdout, "***'%s': memory error during array initialization!\n", funcName);
		freeCharArray(funcName);
		return NULL;
	}
	freeCharArray(funcName);
	return a;
}

void freeIntArray(int *a) {
	free(a);
	a = NULL;
	return;
}

char* initCharArray(int n) {

	char *a = (char*) calloc(n, sizeof(char));
	if(a == NULL) {
		fprintf(stdout, "***'initCharArray': memory error during array initialization!\n");
		return NULL;
	}
	return a;
}

//	Utility function used to free an array memory.
void freeCharArray(char *a) {
	free(a);
	a = NULL;
	return;
}

Line *initLineArray(int n) {
	char *funcName = initCharArray(nameLen);
	sprintf(funcName, "initLineArray");

	Line *a = (Line*) calloc(n, sizeof(Line));
	if(a == NULL) {
		fprintf(stdout, "***'%s': memory error during array initialization!\n", funcName);
		freeCharArray(funcName);
		return NULL;
	}
	freeCharArray(funcName);
	return a;
}

void freeLineArray(Line *a) {
	free(a);
	a = NULL;
	return;
}

int** initMat(int r, int c) {
	char *funcName = initCharArray(nameLen);
	sprintf(funcName, "initMat");

	int i;

	int **mat = (int**) calloc(c, sizeof(int*));
	if (mat == NULL) {
		fprintf(stdout, "***'%s': memory error during adjacency *matrix initialization!\n", funcName);
		freeCharArray(funcName);
		return NULL;
	}
	for (i = 0; i < r; i++) {
		mat[i] = (int*) calloc(c, sizeof(int));
		if (mat[i] == NULL) {
			fprintf(stdout, "***'%s': memory error during adjacency matrix initialization!\n", funcName);
			freeCharArray(funcName);
			return NULL;
		}
	}
	freeCharArray(funcName);
	return mat;
}

void freeMat(int **mat, int r) {
	int i;
	for (i = 0; i < r; i++) {
		free(mat[i]);
		mat[i] = NULL;
	}
	free(mat);
	mat = NULL;
	return;
}

int** initAdjMat(int n) {
	char *funcName = initCharArray(nameLen);
	sprintf(funcName, "initAdjMat");

	int i;

	int **mat = (int**) calloc(n, sizeof(int*));
	if (mat == NULL) {
		fprintf(stdout, "***'%s': memory error during adjacency matrix initialization!\n", funcName);
		freeCharArray(funcName);
		return NULL;
	}
	for (i = 0; i < n; i++) {
		mat[i] = (int*) calloc(2, sizeof(int));
		if (mat[i] == NULL) {
			fprintf(stdout, "***'%s': memory error during adjacency matrix initialization!\n", funcName);
			freeCharArray(funcName);
			return NULL;
		}
	}
	resetAdjMatVals(mat, n);
	return mat;
}

void resetAdjMatVals(int **mat, int n) {
	int i, j;
	for (i = 0; i < n; i++)
		for (j = 0; j < 2; j++)
			mat[i][j] = -1;
	return;
}

void freeAdjMat(int **mat, int n) {
	int i;
	for (i = 0; i < n; i++) {
		free(mat[i]);
		mat[i] = NULL;
	}
	free(mat);
	mat = NULL;
	return;
}

void swapInt(int *i, int *j) {
	int tmp = *j;
	*j = *i;
	*i = tmp;
	return;
}

void swapDbl(double *i, double *j) {
	double tmp = *j;
	*j = *i;
	*i = tmp;
	return;
}
